package com.xiaomi.shwx.common.statemachine;


import com.xiaomi.shwx.common.enums.WorkOrderEvents;
import com.xiaomi.shwx.common.enums.WorkOrderStatus;
import com.xiaomi.shwx.entity.pojo.WorkOrder;
import com.xiaomi.shwx.mapper.WorkOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lei
 * @description 状态机配监听
 */
@Slf4j
@WithStateMachine(name="workOrderStateMachine")
@Component
public class WorkOrderStateListener {

    @Resource
    private WorkOrderMapper workOrderMapper;
    //工程师接单
    @OnTransition(source = "CREATE",target = "BE_CONFIRM")
    public boolean workerAcceptOrder(Message<WorkOrderEvents> message) {
        WorkOrder workOrder = (WorkOrder) message.getHeaders().get("workOrder");
        log.info("状态机监听:{ 订单状态:" + workOrder.getWorkOrderStatus() + "(" + workOrder.getWorkOrderStatus().getDescribe() +"),执行事件:" + message.getPayload().name() + "(" + message.getPayload().getDescription() +") }");
        log.info("工程师接取维修订单");
        workOrder.setWorkOrderStatus(WorkOrderStatus.BE_CONFIRM);
        workOrder.setStatus(WorkOrderStatus.BE_CONFIRM.getStatus());
        workOrderMapper.updateById(workOrder);
        return true;
    }

    @OnTransition(source = "BE_CONFIRM",target = "CONFIRM")
    public boolean userConfirmOrder(Message<WorkOrderEvents> message) {
        WorkOrder workOrder = (WorkOrder) (message.getHeaders().get("workOrder"));
        log.info("状态机监听:{ 订单状态:" + workOrder.getWorkOrderStatus() + "(" + workOrder.getWorkOrderStatus().getDescribe() +"),执行事件:" + message.getPayload().name() + "(" + message.getPayload().getDescription() +") }");
        log.info("用户确认订单");
        workOrder.setWorkOrderStatus(WorkOrderStatus.CONFIRM);
        workOrder.setStatus(WorkOrderStatus.CONFIRM.getStatus());
        workOrderMapper.updateById(workOrder);
        return true;
    }

    @OnTransition(source = "CONFIRM",target = "CHECK")
    public boolean workerCheck(Message<WorkOrderEvents> message) {
        WorkOrder workOrder = (WorkOrder) (message.getHeaders().get("workOrder"));
        log.info("状态机监听:{ 订单状态:" + workOrder.getWorkOrderStatus() + "(" + workOrder.getWorkOrderStatus().getDescribe() +"),执行事件:" + message.getPayload().name() + "(" + message.getPayload().getDescription() +") }");
        log.info("工程师检测");
        workOrder.setWorkOrderStatus(WorkOrderStatus.CHECK);
        workOrder.setStatus(WorkOrderStatus.CHECK.getStatus());
        workOrderMapper.updateById(workOrder);
        return true;
    }

    @OnTransition(source = "CHECK",target = "RECHECK")
    public boolean workerReCheck(Message<WorkOrderEvents> message) {
        WorkOrder workOrder = (WorkOrder) (message.getHeaders().get("workOrder"));
        log.info("状态机监听:{ 订单状态:" + workOrder.getWorkOrderStatus() + "(" + workOrder.getWorkOrderStatus().getDescribe() +"),执行事件:" + message.getPayload().name() + "(" + message.getPayload().getDescription() +") }");
        log.info("工程师申请物料进行维修");
        workOrder.setWorkOrderStatus(WorkOrderStatus.RECHECK);
        workOrder.setStatus(WorkOrderStatus.RECHECK.getStatus());
        workOrderMapper.updateById(workOrder);
        return true;
    }

    @OnTransition(source = "CREATE",target = "CANCEL")
    public boolean userCancelOrderByCreate(Message<WorkOrderEvents> message) {
        WorkOrder workOrder = (WorkOrder) (message.getHeaders().get("workOrder"));
        log.info("状态机监听:{ 订单状态:" + workOrder.getWorkOrderStatus() + "(" + workOrder.getWorkOrderStatus().getDescribe() +"),执行事件:" + message.getPayload().name() + "(" + message.getPayload().getDescription() +") }");
        log.info("用户在创建阶段选择取消订单");
        workOrder.setWorkOrderStatus(WorkOrderStatus.CANCEL);
        workOrder.setStatus(WorkOrderStatus.CANCEL.getStatus());
        workOrderMapper.updateById(workOrder);
        return true;
    }




}